
package com.tools.cleanmaster.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.tools.ad.util.StatusBarUtil;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.ui.base.SingleActivity;
import com.tools.cleanmaster.ui.view.photo.PhotoView;
import com.tools.cleanmaster.ui.view.photo.PhotoViewAttacher;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;
import com.tools.cleanmaster.utils.Utils;

/**
 * desc: 展示某一张图片
 * date: 2018/7/4 11:41
 * author: ancun
 */
public class MediaShowActivity extends SingleActivity {

    public static final String PATH = "path";

    private PhotoView mImageView;

    private Bitmap mBitmap;

    private LinearLayout mLinBroken;

    private static final String TAG = "TrashImageShowActivity";

    private static final int MAX_WIDTH = 1024;//最大宽
    private static final int MAX_HEIGHT = 1280;//最大高

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.trash_image_show_layout);
        StatusBarUtil.setTransparent(this);
        mLinBroken = findViewById(R.id.lin_broken);
        mLinBroken.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mImageView = findViewById(R.id.show_image);
        mImageView.setZoomable(true);
        mImageView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {

            @Override
            public void onViewTap(View view, float x, float y) {
                finish();
            }
        });
        init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBitmap != null) {
            mBitmap.recycle();
        }
    }

    private void init() {
        Intent intent = getIntent();
        String path = "";
        if (intent != null) {
            path = intent.getStringExtra(PATH);
        }

        if (!TextUtils.isEmpty(path)) {
            mBitmap = loadBitmapFromFile(path, Utils.getScreenWidthPixels(),
                    Utils.getScreenHeightPixels());
        }

        if (mBitmap != null) {
            mImageView.setImageBitmap(mBitmap);
        } else {
            mLinBroken.setVisibility(View.VISIBLE);
        }
    }

    private Bitmap loadBitmapFromFile(String path, int screenWidth, int screenHeight) {
        if (FeatureConfig.DEBUG_LOG) {
            LogHelper.i(TAG, "loadBitmapFromFile path=" + path + ",screenWidth=" + screenWidth
                    + ",screenHeight=" + screenHeight);
        }
        // 不能超过最大高与最大宽，避免尺寸太大而OOM
        final int width = Math.min(screenWidth, MAX_WIDTH);
        final int height = Math.min(screenHeight, MAX_HEIGHT);
        if (width <= 0 || height <= 0) {
            return null;
        }

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        if (options.outHeight == -1 || options.outWidth == -1) {
            return null;
        }

        // Calculate inSampleSize，尽量接近width与height
        int inSampleSize = Math.max((int) (0.5f + (float) options.outHeight / (float) height),
                (int) (0.5f + (float) options.outWidth / (float) width));
        inSampleSize += 1; // 强制 +1，防止在某些手机上的OOM
        options.inSampleSize = Math.max(inSampleSize, 1);

        if (FeatureConfig.DEBUG_LOG) {
            LogHelper.i(TAG, path + ", sample=" + options.inSampleSize + ", bmpWidth="
                    + options.outWidth + ", bmpHeight=" + options.outHeight + ", width=" + width
                    + ", height=" + height);
        }

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        // DO NOT ZOOM HERE
        // caller must zoom the bitmap to expected size, it might has its own
        // aspect / size requirement
        return BitmapFactory.decodeFile(path, options);
    }
}
